<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/10/22 0022
 * Time: 15:59
 */
namespace app\api\model;
use think\Db;

class User
{
    private $tableName = 'user';
    private $config    = '';
    public function __construct($token=null)
    {
        if($token){
            $this->config = Db::view('user')
                ->view('vip_price','vip_name,vip_icon','vip_price.id=user.vip','left')
                ->view('occupation','occupation_name','occupation.id=user.occupation','left')
                ->where('token',$token)
                ->json(['answer_number'])
                ->find();
        }
    }

    /**获取user实例
     * @param $token
     * @return array|null|\PDOStatement|string|\think\Model
     */
    public function get($token){
        if(!$this->config){
            $this->config =  Db::view('user')
                ->view('vip_price','vip_name,vip_icon','vip_price.id=user.vip','left')
                ->view('occupation','occupation_name','occupation.id=user.occupation','left')
                ->where('token',$token)
                ->json(['answer_number'])
                ->find();
        }
        return $this->config;
    }

    /**更新user表
     * @param null $user
     * @return bool
     */
    public function Update($user=null){
        if(!$user&&!$this->config){
            return false;
        }
        if(!$user&&$this->config){
            $user = $this->config;
        }
        if(isset($user['occupation_name'])){
            unset($user['occupation_name']);
        }
        if(isset($user['vip_name'])){
            unset($user['vip_name']);
        }
        if (isset($user['vip_icon'])){
            unset($user['vip_icon']);
        }
//        return $user;
        if(Db::name($this->tableName)->json(['answer_number'])->where('id',$user['id'])->update($user)){
            return true;
        }else{
            return false;
        }
    }
}